﻿using Common.Contract.Authentication.Record;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Customer.Core.ServiceCommand
{
    public class RetrieveCustomerLoanMatdtServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveCustomerAlertRequest, RetrieveCustomerLoanMatdtResponse>
    {
        #region Properties
        public UserRecord Dao;
        public FilterRecord TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveCustomerLoanMatdtServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveCustomerAlertRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            TextSearch = Request.Filter;
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrieveCustomerLoanMatdtResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<AlertCustomerRecord> records = new List<AlertCustomerRecord>();
            List<LoanMatdt> listCustomer = new List<LoanMatdt>();
            Dictionary<string, Branch> listBranches = Providers.Branchs.RetrieveAllBranches().ToDictionary(x => x.BranchId);
            listCustomer = Providers.CustomerAlerts.RetrieveCustomerLoanMatdt(out totalRecord, Dao, Request.Lang, TextSearch, pageSize, pageIndex);
            if (listCustomer != null)
            {
                records = listCustomer.Select(x => new AlertCustomerRecord
                {
                    Zone = listBranches.ContainsKey(x.BranchId) ? listBranches[x.BranchId].Region : string.Empty,
                    DaoName = x.DaoName,
                    Dao = x.DaoCif,
                    Branch = listBranches.ContainsKey(x.BranchId) ? listBranches[x.BranchId].BranchNameSme : string.Empty,
                    Cif = x.Cif,
                    CustName = x.CustomerName,
                    Industry = x.VpbIndustryName,
                    Acctno = x.Acctno,
                    BalQd = x.BalQd.ToString("#,###", cul.NumberFormat),
                    Matdt = x.Matdt,
                }).ToList();
            }
            response.Records = records;
            response.Page = new PageRecord
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                TotalRecords = totalRecord
            };
            return isSuccess;
        }
    }
}
